<?php
/*
  新闻发布内容.
 */

$form = $this->beginWidget('CActiveForm', array(
    "id" => "saveManageEventForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
$url = $control->getAjaxLinkUrl('saveManageEventContent', NULL, array(
    'eventId' => $eventId, 'manageView' => $manageView, 'contentId' => $contentId));
$updateId = $control->getAjaxContainerId();
?>
<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-tooltip.js"/>
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-popover.js"/>

<script type="text/javascript">
    $(document).ready(function() {
        $("a[rel='tooltip']").tooltip({'placement': 'right', 'z-index': '3000'});
    });
</script>
</script>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function addManageEvent<?php echo $eventId; ?>() {
        var newsTitle = $('#BDynamicFormModel_media_newsTitle_event_info').val();
        var eventInfo = $('#BDynamicFormModel_media_info_event_info').val();
        var eventAutho = $('#BDynamicFormModel_media_author_event_info').val();
        var filepath = $('#BDynamicFormModel_image_media_image_event_info').val();
        var extStart = filepath.lastIndexOf(".");
        var ext = filepath.substring(extStart, filepath.length).toUpperCase();
        if (newsTitle != "") {
            if (eventInfo != "") {
                if (eventAutho != "") {
                    if (filepath != "") {
                        if (ext == ".BMP" || ext == ".PNG" || ext == ".GIF" || ext == ".JPG" || ext == ".JPEG") {
                            var formData = new FormData($("#saveManageEventForm")[0]);
                            $.ajax({
                                url: '<?php echo $url; ?>',
                                type: 'POST',
                                data: formData,
                                //datatype: 'json',
                                // async: false,
                                beforeSend: function() {
                                    $("#ajax-loaderdiv").show();
                                },
                                success: function(html) {
                                    // on success do some validation or refresh the content div to display the uploaded images 
                                    jQuery("#<?php echo $updateId; ?>").html(html);
                                },
                                complete: function() {
                                    $("#ajax-loaderdiv").hide();
                                },
                                error: function(data) {
                                    alert("There may a error on uploading. Try again later");
                                },
                                cache: false,
                                contentType: false,
                                processData: false
                            });
                        } else
                            alert("图片限于bmp,png,gif,jpeg,jpg格式");
                    } else
                        alert("请上传图片")
                } else
                    alert("请输入作者");
            } else
                alert("请输入新闻详情");
        } else
            alert("请输入新闻标题");

        return false;
    }
</script>
<script type="text/javascript">
    $(function() {
        $('#activeTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#expireTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#offsetOfBookingStartTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#offsetOfBookingEndTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#timeSlotStartTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
//        $('#datetimepicker1').datetimepicker({
//          language: 'pt-BR'                   //该样式可以显示为：2014-07-16 22:45:03，需要修改时间样式：data-format="yyyy-MM-dd hh:mm:ss"
//        });
    });


// this script for collecting the form data and pass to the controller action and doing the on success validations
    function saveBookableTimeSlot() {

        var formData = new FormData($("#message-form")[0]);
        //var formElement = document.getElementById("message-form");
        //var formData = new FormData(formElement);

        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(data);
                //alert(data);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
                $("#myModal").modal("hide");
            },
            error: function() {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }

</script>
<legend>
    <h4>
        <?php echo $control->getAjaxLink(array('name' => '活动列表', 'id' => 'showManage')); ?>\
        <?php echo $control->getAjaxLink(array('name' => '管理活动', 'id' => 'manageEvent'), array('eventId' => $eventId, 'manageView' => $manageView)); ?>\
        <?php echo '管理活动内容'; ?>
    </h4>
</legend>

<?php
if (!isset($model))
    $model = new BDynamicFormModel ();

//$eventNameField = 'media_name_event_info';
//$eventDescriptionField = 'media_description_event_info';
$newsImageField = 'image_media_image_event_info';

//$eventTypeField = 'media_eventTypeId_event_info';
$newsTitleField = 'media_newsTitle_event_info';
$newsInfoField = 'media_info_event_info';
$newsAuthorField = 'media_author_event_info';
$model->addField($newsTitleField);
$model->addField($newsInfoField);
$model->addField($newsImageField);
$model->addField($newsAuthorField);
$eventTable = new BAEventSheet();
$event = $eventTable->getListById($eventId);


if ($contentId > 0) {
//$eventTable = new BAEventSheet();
//$eventContent = $eventTable->getListById($contentId);
    $manageEventContentTable = new BAEventSheet();
    $eventContent = $manageEventContentTable->getListById($contentId);
    $eventContentView = $manageEventContentTable->getView();
    $newsTitleItem = $eventContent->getItem('newsTitle');
    $imageItem = $eventContent->getItem('image');
    $infoItem = $eventContent->getItem('info');
    $authorItem = $eventContent->getItem('author');
    if ($newsTitleItem instanceof BTreeItem) {
        $name = $newsTitleItem->getValue();
        $model->$newsTitleField = $name;
    }

    if ($imageItem instanceof BTreeItem) {
        $image = $imageItem->getValue();
        $model->$newsImageField = $image;
    }
    if ($infoItem instanceof BTreeItem) {
        $info = $infoItem->getValue();
        $model->$newsInfoField = $info;
    }
    if ($infoItem instanceof BTreeItem) {
        $info = $infoItem->getValue();
        $model->$newsInfoField = $info;
    }
    if ($authorItem instanceof BTreeItem) {
        $author = $authorItem->getValue();
        $model->$newsAuthorField = $author;
    }
}
$eventName = $event->getItem('name')->getValue();
?>
<?php
$app = Blyn::app()->getActiveApp();
?>

<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo $eventName ?></th>
            <th></th>
            <th><?php echo '说明'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <?php echo '新闻标题'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $newsTitleField, array('placeholder' => 'newsTitle', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td></td>
        </tr>
        <tr>
            <td>
                <?php echo '作者'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $newsAuthorField, array('placeholder' => 'author', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td></td>
        </tr>
        <tr><td> <?php echo '新闻内容'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextArea($model, $newsInfoField, array('placeholder' => 'info' . '...', 'required' => true));
                ?>
            </td>
            <td></td>
        </tr>  
        <tr><td> <?php echo '新闻图片' ?> </td>
            <td>
                <?php
                echo CHtml::activeFileField($model, $newsImageField, array('placeholder' => 'image' . '...', 'required' => true));
                if ($model->$newsImageField != NULL) {
                    $imageUrl = Yii::app()->request->baseUrl . $model->$newsImageField;
                    echo "<img src=" . $imageUrl . " width=50 height=50 />";
                }
                ?>
            </td>
            <td></td>
        </tr>   

    </tbody>
</table>
<?php
$this->endWidget();
//echo CHtml::endForm();
?>
<button class="btn" onclick="javascript:addManageEvent<?php echo $eventId; ?>()"><?php echo '保存' ?></button>

